﻿package bdz.projects.lavender.doi.ui
{	
	import bdz.common.events.*;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.utils.*;
	import gs.*;
	import gs.easing.*;
	/**
	 * ...
	 * @author ...
	 */
	public dynamic class DOIQuickMenuItem extends MovieClip 
	{
		private var _local:Object;
		private var _renderer:String;
		private var _textLabel:TextField;
		private var _hitter:MovieClip;
		public var index:Number;
		public var indent:Number = 0;
		public var defwidth:Number;
		private var itemRef:Array;
		private var highlight:MovieClip;
		private var subholder:MovieClip;
		private var filt:DropShadowFilter;
		private var lastopened:DOIQuickMenuItem;
		private var _grouped:Boolean;
		public function DOIQuickMenuItem() {
			
			super();
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		private function initializeComponent(evt:Event):void {
			_textLabel = this["tlabel"];
			
			_textLabel.text = _local.name.split("|").join(" ");
			_textLabel.x = indent;
			_hitter = new MovieClip();
			_hitter.buttonMode = _hitter.useHandCursor = true;
			_hitter.graphics.beginFill(0, 0);
			_hitter.graphics.drawRect(0, 0, defwidth, _textLabel.height);
			_hitter.graphics.endFill();
			_hitter.graphics.endFill();
			addChild(_hitter);
			attachButtonActions();
			highlight = new MovieClip();
			var box:MovieClip = new MovieClip();
			box.graphics.beginFill(0xffffff, 1);
			box.graphics.drawRect(0, 0, defwidth, _hitter.height + 10);
			
			highlight.y = (_hitter.height) / 2;
			highlight.addChild(box);
			box.y = -(_hitter.height) / 2;
			addChild(highlight);
			setChildIndex(highlight, 0);
			highlight.scaleY = 0;
			
			//highlight.filters = [new DropShadowFilter(0, 90, 0x000000, .1, 20, 20, 1, 3)];
			swapChildren(this["bg"], highlight);
			
		}
		private function attachButtonActions():void {
			_hitter.addEventListener(MouseEvent.ROLL_OVER, rollActions);
			_hitter.addEventListener(MouseEvent.ROLL_OUT, rollActions);
			_hitter.addEventListener(MouseEvent.CLICK, hitAction);
		}
		private function removeButtonActions():void {
			_hitter.removeEventListener(MouseEvent.ROLL_OVER, rollActions);
			_hitter.removeEventListener(MouseEvent.ROLL_OUT, rollActions);
			_hitter.removeEventListener(MouseEvent.CLICK, hitAction);
			TweenLite.to(highlight, .6, { scaleY:0, ease:Strong.easeOut } );
		}
		private function hitAction(evt:MouseEvent):void {
			removeButtonActions();
			TweenLite.to(highlight, .6, { scaleY:1, ease:Strong.easeOut } );
			dispatchEvent(new GenericEvent(GenericEvent.CLICKED, dataProvider));
		}
		private function onItemSelect(evt:GenericEvent):void {
			if (lastopened) {
				lastopened.closeItems();
			}
			lastopened = evt.target as DOIQuickMenuItem;
		}
		
		private function rollActions(evt:MouseEvent):void {
			if (evt.type == MouseEvent.ROLL_OVER) {
				dispatchEvent(new GenericEvent("genRollOver", dataProvider));
				//parent.setChildIndex(this, parent.numChildren - 1);
				TweenLite.to(highlight, .6, { scaleY:1, ease:Strong.easeOut } );
			}else {
				TweenLite.to(highlight, .6, { scaleY:0, ease:Strong.easeOut } );
			}
		}
		public function activate():void {
			attachButtonActions();
			TweenLite.to(highlight, .6, { scaleY:0, ease:Strong.easeOut } );
		}
		public function closeItems():void {
			_hitter.visible = true;
			TweenLite.to(subholder, .1, {alpha:0, ease:Strong.easeOut} );
		}
		public function get compactHeight():Number {
			return _hitter.height;
		}
		public function set dataProvider(o:Object):void {
			_local = o;
		}
		public function get dataProvider():Object {
			return _local;
		}
		public function get subItems():Array {
			if(_local[_local.subtype]){
				return _local[_local.subtype];
			}
			return null;
		}
		public function set renderer(str:String):void {
			_renderer = str;
		}
		public function get renderer():String {
			return _renderer;
		}
	}
	
}